﻿using System;
using System.Web;
using System.Web.SessionState;
using System.Collections.Generic;
using System.Xml;
using System.Data;

using Gift.DAL;
using Gift.Model;
using Gift.WebApp;

namespace WebApp.cn.ashx
{
    public class loginHandler : IHttpHandler, IRequiresSessionState
    {
        /// <summary>
        /// 您将需要在您网站的 web.config 文件中配置此处理程序，
        /// 并向 IIS 注册此处理程序，然后才能进行使用。有关详细信息，
        /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        // Fields
        protected string basicSetXmlPath = "/cn/xml/website_set.xml";
        BasePage thePage = new BasePage();
        tbUser oUser = new tbUser();

        public bool IsReusable
        {
            // 如果无法为其他请求重用托管处理程序，则返回 false。
            // 如果按请求保留某些状态信息，则通常这将为 false。
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            if (string.Compare(context.Request.Form["code"], context.Session["mlCheckCode"].ToString(), true) != 0)
            {
                context.Response.Write("0");
            }
            else if (BaseFactory.theUser.DetectionUser(context.Request.Form["userName"]))
            {
                context.Response.Write("1");
            }
            else
            {
                this.oUser.userName = context.Request.Form["userName"];
                this.oUser.userPwd = Gift.Security.DESEncrypt.Encrypt(context.Request.Form["userPwd"]);
                DataTable table = BaseFactory.theUser.UserLogin(this.oUser);
                if (table.Rows.Count > 0)
                {
                    DataRow row = table.Rows[0];
                    if (Convert.ToInt32(row["state"]) == 0)
                    {
                        context.Response.Write("3");
                    }
                    else
                    {
                        string str;
                        string str2;
                        this.oUser.id = Convert.ToInt32(row["id"]);
                        this.oUser.email = row["email"].ToString();
                        this.oUser.userRank = Convert.ToInt32(row["rankId"]);
                        this.oUser.RankName = row["rankName"].ToString();
                        this.oUser.money = Convert.ToDecimal(row["money"]);
                        this.oUser.payPoints = Convert.ToInt32(row["payPoints"]);
                        thePage.LoginUserMember = new UserMember();
                        thePage.LoginUserMember.ClientUser = this.oUser;
                        thePage.LoginUserMember.MemberId = this.oUser.id.ToString();
                        if (HttpContext.Current.Request.Cookies["shopping_cart_mac"] != null)
                        {
                            str = HttpContext.Current.Request.Cookies["shopping_cart_mac"].Value;
                            str2 = this.oUser.id.ToString();
                            BaseFactory.theShoppingCart.ModifyShoopingCartUser(str, str2);
                        }
                        if (HttpContext.Current.Request.Cookies["group_buy_shopping_cart_mac"] != null)
                        {
                            str = HttpContext.Current.Request.Cookies["group_buy_shopping_cart_mac"].Value;
                            str2 = this.oUser.id.ToString();
                            BaseFactory.theGroupBuyShoppingCart.ModifyGroupBuyShoopingCartUser(str, str2);
                        }
                        string attribute = "";
                        XmlDocument document = new XmlDocument();
                        document.Load(HttpContext.Current.Server.MapPath(this.basicSetXmlPath));
                        attribute = ((XmlElement)document.SelectSingleNode("set/cn_title")).GetAttribute("value");
                        int inBoxNoIsReadByAddressee = BaseFactory.theInBox.GetInBoxNoIsReadByAddressee(thePage.LoginUserMember.ClientUser.userName);
                        context.Response.Write(string.Concat(new object[] { "您好，<span class=\"red\">", thePage.LoginUserMember.ClientUser.userName, "</span>，欢迎光临 <strong>", attribute, "</strong>！&nbsp;<a href=\"/cn/member_center/my_order.html\">[&nbsp;会员中心&nbsp;]</a>&nbsp;<a class=\"mail\" href=\"/cn/member_center/in_box.html\">(<strong>", inBoxNoIsReadByAddressee, "</strong>)</a>&nbsp;<a href=\"userExitHandler.ashx\">[&nbsp;退出&nbsp;]</a>" }));
                    }
                }
                else
                {
                    context.Response.Write("2");
                }
            }
        }

        #endregion
    }
}
